package com.mobilepay.moneypig.base;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.inject.InjectUtil;
import com.mobilepay.moneypig.view.CustomProgressDialog;

/**
 * Created by xlm on 2017/7/20 0020
 */
public abstract class BaseDialog extends Dialog {
    /**
     * 因为Dialog将构造方法内的context转成ContextThemeWrapper，
     * 之后会造成context无法再转成activity，所以此处自己将context赋值给activity
     * 并提供getActivity方法
     */
    private BaseActivity mActivity;

    private BaseDialog mThis;

    public BaseDialog(Context context) {
        super(context, R.style.CommonDialog);
        mActivity = (BaseActivity) context;
    }

    public BaseDialog(Context context, int themeResId) {
        super(context, themeResId);
        mActivity = (BaseActivity) context;
    }

    protected BaseDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        mActivity = (BaseActivity) context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        beforeSetContentView();
        onSetContentView();
        afterSetContentView();
        onFindView();
        injectView();
        initViews();
        afterOnCreate(savedInstanceState);
        onSetListener();
    }

    @Override
    public void cancel() {
        super.cancel();
    }

    @Override
    public void dismiss() {
        if (beforeCancel()) {
            return;
        }
        super.dismiss();
    }

    public void dismissIgnore() {
        super.dismiss();
    }

    /**
     * 在dismiss之前执行
     *
     * @return true 会屏蔽dismiss方法, false会继续执行dismiss方法
     */
    protected boolean beforeCancel() {
        return false;
    }

    public BaseActivity getActivity() {
        return mActivity;
    }

    public BaseDialog getThis() {
        return mThis;
    }

    /**
     * 通过注解自动绑定资源ID
     */
    private void injectView() {
        InjectUtil.injectDialogFields(this);
    }

    protected void beforeSetContentView() {

    }

    protected abstract void onSetContentView();

    protected void afterSetContentView() {

    }

    protected void onFindView() {
    }

    protected abstract void initViews();

    protected abstract void onSetListener();

    protected abstract void afterOnCreate(Bundle savedInstanceState);


    /**
     * @param widthscale  宽度占屏幕宽度比例
     * @param heightscale 宽度占屏幕宽度比例
     * @param gravity
     */
    public void initWindow(float widthscale, float heightscale, int gravity) {
        initWindow(widthscale, heightscale, gravity, true);
    }

    /**
     * @param widthscale  宽度占屏幕宽度比例
     * @param heightscale 宽度占屏幕宽度比例
     */
    public void initWindow(float widthscale, float heightscale) {
        initWindow(widthscale, heightscale, Gravity.CENTER, true);
    }

    /**
     * @param widthscale  宽度占屏幕宽度比例
     * @param heightscale 宽度占屏幕宽度比例
     * @param gravity
     * @param outcancel   是否点击外部消失
     */
    public void initWindow(float widthscale, float heightscale, int gravity, boolean outcancel) {
        Window window = getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        if (gravity == Gravity.NO_GRAVITY) {
            window.setGravity(Gravity.CENTER);
        } else {
            window.setGravity(gravity);
        }
        window.setBackgroundDrawableResource(R.drawable.shape_transparent);
        setCanceledOnTouchOutside(outcancel);
        if (widthscale > 0) {
            int width = CommonUtil.getDisplayMetrics(getContext()).widthPixels;
            lp.width = (int) (widthscale * width);
        }
        if (heightscale > 0) {
            int height = CommonUtil.getDisplayMetrics(getContext()).heightPixels;
            lp.height = (int) (heightscale * height);
        }
        window.setAttributes(lp);
    }

    public void setGravity(int gravity) {
        Window window = getWindow();
        if (gravity == Gravity.NO_GRAVITY) {
            window.setGravity(Gravity.CENTER);
        } else {
            window.setGravity(gravity);
        }
    }

    public void setAnimate(@StyleRes int id) {
        Window window = getWindow();
        window.setWindowAnimations(id);
    }

    public void showToastShort(String msg) {
        getActivity().showToastShort(msg);
    }

    public void showToastLong(String msg) {
        getActivity().showToastLong(msg);
    }

    public CustomProgressDialog getProgressDialog(String msg) {
        return getActivity().getDlProgressDialog(msg);
    }

    public void showProgress(String msg) {
        getActivity().showProgress(msg);
    }

    public void dismissProgress() {
        getActivity().dismissProgress();
    }
}
